package tts.controller.usrdetails;

import org.springframework.beans.factory.annotation.Autowired;

import tts.controller.RedirectAction;
import tts.dao.UsersHome;
import tts.dao.UsrDetailsHome;
import tts.model.UsrDetails;

public class Delete extends RedirectAction{

	//fields
	private static final long serialVersionUID = -4408150693799196480L;
	@Autowired
	private UsrDetailsHome usrDetailsHome;
	@Autowired
	private UsersHome usersHome;
	private int usrDetails_id;
	private String username;

	//get & set
	public int getUsrDetails_id() {
		return usrDetails_id;
	}
	public void setUsrDetails_id(int usrDetails_id) {
		this.usrDetails_id = usrDetails_id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}

	public String delete() {
		try {
			UsrDetails usrDet = usrDetailsHome.findById(this.usrDetails_id);
			usersHome.findByUsername(this.username).setUsrDetails(null);
			usrDetailsHome.delete(usrDet);
		} catch (RuntimeException e) {
			addActionError(e.getMessage());
			return ERROR;
		}
		addActionMessage(getText("details.deleted"));
		return SUCCESS;
	}	
}